package com.backend.controller;

import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.backend.common.AddAction;
import com.backend.common.AssAdminUpdateInfor;
import com.backend.common.GetAllUserContion;
import com.backend.common.GetAssActionCond;
import com.backend.config.Result;
import com.backend.service.AdminService;
import com.backend.service.AssAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/user")
public class AssAdminController {

    @Autowired
    AdminService adminService;

    @Autowired
    AssAdminService assAdminService;


    @GetMapping("/getAssById")
    Result getAssById(@RequestParam("assId")String assId){
        return assAdminService.getAssById(assId);
    }

    @GetMapping("/deleteAssImageById")
    Result deleteAssImageById(@RequestParam("assId")String assId){
        return assAdminService.deleteAssImageById(assId);
    }

    @GetMapping("/passUserAddAction")
    Result passUserAddAction(@RequestParam("stuId")String stuId,@RequestParam("status")String status,@RequestParam("actionId")String actionId){
        return assAdminService.passUserAddAction(stuId,status,actionId);
    }

    @PostMapping("/updateAssInforAdmin")
    Result updateAssInforAdmin(@RequestBody AssAdminUpdateInfor assAdminUpdateInfor){
        return assAdminService.updateAssInforAdmin(assAdminUpdateInfor);
    }

    @PostMapping("/getAssActionByCond")
    Result getAssActionByCond(@RequestBody GetAssActionCond getAssActionCond){
        return assAdminService.getAssActionByCond(getAssActionCond);
    }


    @PostMapping("/addAssActionByAssId")
    Result addAssActionByAssId(@RequestBody AddAction addAction){
        return assAdminService.addAssActionByAssId(addAction);
    }




    @GetMapping("/getActionStudentBuId")
    Result getActionStudentBuId(@RequestParam("actionId")String actionId){
        return assAdminService.getActionStudentBuId(actionId);
    }



    @PostMapping("/studentExport")
    Result studentExport(@RequestBody GetAllUserContion getAllUserContion){

        Result result=adminService.getAssAllUserByAssId(getAllUserContion);

        List<Map<String,String>> mapList= (List<Map<String, String>>) result.getData();

        if(mapList.size()==0){
            return Result.fail("该社团成员为空，导出失败",0);
        }

        //        创建表头;
        List<String> column=new ArrayList<>();
        column.add("序号");
        column.add("班级");
        column.add("学号");
        column.add("姓名");
        column.add("性别");
        column.add("年龄");
        column.add("电话号码");

        Map<String,String> assMap=assAdminService.getAssNameById(getAllUserContion.getAssId());
        //表头对应的数据
        List<Map<String,Object>> data = new ArrayList<>();
        for (Map<String,String> map:mapList) {

            Map<String,Object> dataMap = new HashMap<>();
            dataMap.put("序号",map.get("id"));
            dataMap.put("班级",map.get("stu_class"));
            dataMap.put("学号",map.get("stu_id"));
            dataMap.put("姓名",map.get("stu_name"));
            dataMap.put("性别",map.get("stu_sex"));
            dataMap.put("年龄",map.get("stu_age"));
            dataMap.put("电话号码",map.get("stu_telephone"));
            data.add(dataMap);
        }
        ExcelWriter writer = ExcelUtil.getWriter("d:/"+assMap.get("ass_name")+".xlsx");
        writer.write(data, true);
        writer.close();
        return Result.success("导出成功",1);
    }
}
